package com.medical.data.service.impl;

import com.medical.data.domain.User;
import com.medical.data.exception.MyException;
import com.medical.data.service.UserService;
import com.medical.data.vo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("userDetailsService")
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

//    @Autowired
//    private AuthorityService authorityService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户
        User user = userService.getUserByName(username);
        if (ObjectUtils.isEmpty(user))
            throw new MyException(500,"用户没有找到");
        //TODO 进行权限封装
        List<String> authorites = new ArrayList<>();
        authorites = Arrays.asList("1","2");
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        loginUser.setPermissions(authorites);
        return loginUser;
    }
}
